import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { saveAs } from 'file-saver';

@Injectable()
export class HotlineService {
    constructor(private http: HttpClient) { }

    /** 解析excel文件 */
    analysisFile(file: any) {
        return this.http.post('/hotline/api/analysis', file).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 装载excel文件 */
    importData(items: any) {
        return this.http.post('/hotline/api/import', items).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 数据计算 Calculate */
    calculate(params: any) {
        return this.http.post('/hotline/api/stat/calculate', params).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 数据反查 */
    loadDetail(params: any, pageSize = 30, pageNum = 1) {
        return this.http.post('/hotline/api/stat/detail?pageSize=' + pageSize + '&pageNum=' + pageNum, params).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**数据导出 */
    export(params: any) {
        return this.http.post('/hotline/api/export/doc', params, { 'responseType': 'blob' }).pipe(
            map(res => saveAs(res, "东城区市民热线每日专报.doc")));
    }

    /**导出模板中的问题数据 */
    exportErrorData(params : any) {
        return this.http.post('/hotline/api/exporterror', params, { 'responseType': 'blob' }).pipe(
            map(res => saveAs(res, "问题数据.xlsx")));
    }

    /**综合查询接口 */
    search(params : any){
        return this.http.post('/hotline/api/search/list', params).pipe(
            map((res: Response) => res['data'])
        );
    }
    /** 装载地图 */
    loadMap() {
        return this.http.get('/assets/data/hotline/110101.json');
    }

    /** 装载反查数据 */
    loadCleanItems(params : any){
        return this.http.post('/hotline/api/clean/list', params).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 清除数据 */
    clean(params : any){
        return this.http.post('/hotline/api/clean/submit', params).pipe(
            map((res: Response) => res['data'])
        );
    }
}